package com.itheima.train.manager.service;

import com.alibaba.fastjson.JSON;
import com.itheima.train.manager.domain.Order;
import com.itheima.train.manager.domain.OrderSynEs;
import com.itheima.train.manager.domain.TbRidingPlanDate;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class EsIndexService {

    @Autowired
    private RestHighLevelClient restHighLevelClient ;

    // 把我们的乘车计划数据存储到ES中
    public void addDocument(TbRidingPlanDate tbRidingPlanDate) throws IOException {

        // 创建IndexRequest对象
        IndexRequest indexRequest = new IndexRequest() ;
        indexRequest.index("riding_plan_date") ;
        indexRequest.id(String.valueOf(tbRidingPlanDate.getId())) ;
        indexRequest.source(JSON.toJSONString(tbRidingPlanDate) , XContentType.JSON) ;

        // 创建索引
        restHighLevelClient.index(indexRequest , RequestOptions.DEFAULT) ;

    }

    // 同步库存的扣减操作
    public void synStockInfo(OrderSynEs orderSynEs) throws IOException {

        // 创建搜索请求对象
        SearchRequest searchRequest = new SearchRequest("riding_plan_date") ;
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().trackTotalHits(true) ;// 查询所有满足条件的数据
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery() ;
        boolQueryBuilder.must(QueryBuilders.termQuery("trainNum" , orderSynEs.getTrainNum())) ;
        boolQueryBuilder.must(QueryBuilders.termQuery("trainRidingDate" , orderSynEs.getRidingPlanDate())) ;
        searchSourceBuilder.query(boolQueryBuilder) ;
        searchSourceBuilder.size(1000) ;
        searchRequest.source(searchSourceBuilder) ;

        // 进行搜索
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits searchHits = searchResponse.getHits();
        SearchHit[] hits = searchHits.getHits();
        for(SearchHit searchHit : hits) {
            String sourceAsString = searchHit.getSourceAsString();
            TbRidingPlanDate ridingPlanDate = JSON.parseObject(sourceAsString, TbRidingPlanDate.class);
            switch (orderSynEs.getSeatNature()) {
                case "3" :
                    ridingPlanDate.setFirstSeatStock(ridingPlanDate.getFirstSeatStock() - 1);
                    break ;
                case "4":
                    ridingPlanDate.setSecondSeatStock(ridingPlanDate.getSecondSeatStock() - 1);
                    break;
                case "7":
                    ridingPlanDate.setHardSleeperStock(ridingPlanDate.getHardSleeperStock() - 1);
                    break ;
                case "6":
                    ridingPlanDate.setSoftSleeperStock(ridingPlanDate.getSoftSleeperStock() - 1);
                    break;
                case "2":
                    ridingPlanDate.setBusSeatStock(ridingPlanDate.getBusSeatStock() - 1);
                    break ;
                case "5":
                    ridingPlanDate.setHardSeatStock(ridingPlanDate.getHardSeatStock() - 1);
                    break ;
                case "9":
                    ridingPlanDate.setSoftSeatStock( ridingPlanDate.getSoftSeatStock() - 1);
                    break ;
            }

            // 创建一个更新的请求对象
            UpdateRequest updateRequest = new UpdateRequest("riding_plan_date" , searchHit.getId()).doc(JSON.toJSONString(ridingPlanDate) , XContentType.JSON) ;
            restHighLevelClient.update(updateRequest , RequestOptions.DEFAULT) ;
        }
    }

    // 库存的回退
    public void backStock(Order order) throws IOException {

        // 创建搜索请求对象
        SearchRequest searchRequest = new SearchRequest("riding_plan_date") ;
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().trackTotalHits(true) ;// 查询所有满足条件的数据
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery() ;
        boolQueryBuilder.must(QueryBuilders.termQuery("trainNum" , order.getTrainNum())) ;
        boolQueryBuilder.must(QueryBuilders.termQuery("trainRidingDate" , order.getTrainRidingDate())) ;
        searchSourceBuilder.query(boolQueryBuilder) ;
        searchSourceBuilder.size(1000) ;
        searchRequest.source(searchSourceBuilder) ;

        // 进行搜索
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits searchHits = searchResponse.getHits();
        SearchHit[] hits = searchHits.getHits();
        for(SearchHit searchHit : hits) {
            String sourceAsString = searchHit.getSourceAsString();
            TbRidingPlanDate ridingPlanDate = JSON.parseObject(sourceAsString, TbRidingPlanDate.class);
            switch (order.getSeatNature()) {
                case "3" :
                    ridingPlanDate.setFirstSeatStock(ridingPlanDate.getFirstSeatStock() + 1);
                    break ;
                case "4":
                    ridingPlanDate.setSecondSeatStock(ridingPlanDate.getSecondSeatStock() + 1);
                    break;
                case "7":
                    ridingPlanDate.setHardSleeperStock(ridingPlanDate.getHardSleeperStock() + 1);
                    break ;
                case "6":
                    ridingPlanDate.setSoftSleeperStock(ridingPlanDate.getSoftSleeperStock() + 1);
                    break;
                case "2":
                    ridingPlanDate.setBusSeatStock(ridingPlanDate.getBusSeatStock() + 1);
                    break ;
                case "5":
                    ridingPlanDate.setHardSeatStock(ridingPlanDate.getHardSeatStock() + 1);
                    break ;
                case "9":
                    ridingPlanDate.setSoftSeatStock( ridingPlanDate.getSoftSeatStock() + 1);
                    break ;
            }

            // 创建一个更新的请求对象
            UpdateRequest updateRequest = new UpdateRequest("riding_plan_date" , searchHit.getId()).doc(JSON.toJSONString(ridingPlanDate) , XContentType.JSON) ;
            restHighLevelClient.update(updateRequest , RequestOptions.DEFAULT) ;
        }
    }
}
